package org.biblicalcontent.ui.generiek;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;

public class ExtentionReader {

	private final String extensionId;
	private final String classAttributeName;

	public ExtentionReader(String extensionId, String classAttributeName) {
		this.extensionId = extensionId;
		this.classAttributeName = classAttributeName;

	}

	/**
	 * 
	 */
	@SuppressWarnings("unchecked")
	public <T> List<T> loadExtensions(Class<T> clazz) {
		IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(extensionId);
		try {
			List<T> result = new ArrayList<T>();

			for (IConfigurationElement e : config) {
				Object o = e.createExecutableExtension(classAttributeName);
				if (clazz.isAssignableFrom(o.getClass())) {
					result.add((T) o);
				}
			}
			return result;
		} catch (CoreException ex) {
			throw new IllegalStateException(ex);
		}
	}

}
